<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author ThinkGem
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'sys:empUser:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm">
      <template #teacherResume>
        <BasicTable
          @register="registerEmployeeOfficeTable"
          @row-click="handleEmployeeOfficeRowClick"
        />
        <a-button class="mt-2" @click="handleEmployeeOfficeAdd">
          <Icon icon="ant-design:plus-circle-outlined" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #userTeacher>
        <BasicTable @register="registerUserTeacherTable">
          <template #userRole="{ column, record }">
            <DictLabel
              v-if="record.sportsScoreGrade"
              :dictType="column.dictType"
              :dictValue="record.relationCode"
            />
          </template>
        </BasicTable>
        <a-button class="mt-2" @click="handleParentsOfficeAdd">
          <Icon icon="ant-design:plus-circle-outlined" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #userRoleString>
        <BasicTable @register="registerUserRoleTable">
          <template #userRole="{ column, record }">
            <DictLabel
              v-if="record.sportsScoreGrade"
              :dictType="column.dictType"
              :dictValue="record.relationCode"
            />
          </template>
        </BasicTable>
        <!-- <a-button class="mt-2" @click="handleParentsOfficeAdd">
          <Icon icon="ant-design:plus-circle-outlined" /> {{ t('新增') }}
        </a-button> -->
      </template>
    </BasicForm>
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsSysEmpUserForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { CropperAvatar } from '/@/components/Cropper';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { EmpUser, checkEmpNo, empUserSave, empUserForm } from '/@/api/sys/empUser';
  import { StudentUser, studentUserSave, studentUserForm } from '/@/api/vke/studentUser';
  import { TeacherUser, teacherUserSave, teacherUserForm } from '/@/api/vke/teacherUser';
  import { checkLoginCode } from '/@/api/sys/user';
  import { officeTreeData } from '/@/api/sys/office';
  import { companyTreeData } from '/@/api/sys/company';
  import { roleTreeData } from '/@/api/sys/role';
  import { BasicTable, useTable } from '/@/components/Table';
  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('sys.empUser');
  const { showMessage } = useMessage();
  const record = ref<StudentUser>({} as StudentUser);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增用户') : t('编辑用户'),
  }));
  const ctrlPermi = ref<String>('');
  const op = ref<String>('');
  const avatar = ref('');

  const inputFormSchemas: FormSchema[] = [
    // {
    //   label: t('归属机构'),
    //   field: 'employee.office.officeCode',
    //   fieldLabel: 'employee.office.officeName',
    //   component: 'TreeSelect',
    //   required: true,
    //   ifShow: () => op.value === 'add' || op.value === 'edit',
    // },
    // {
    //   label: t('归属公司'),
    //   field: 'employee.company.companyCode',
    //   fieldLabel: 'employee.company.companyName',
    //   component: 'TreeSelect',
    //   componentProps: {
    //     allowClear: true,
    //   },
    //   ifShow: () => op.value === 'add' || op.value === 'edit',
    // },
    {
      label: t('登录账号'),
      field: 'loginCode',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
      rules: [
        { required: true },
        { min: 4, max: 20, message: t('请输入长度在 4 到 20 个字符之间') },
        { pattern: /^[\u0391-\uFFE5\w]+$/, message: t('不能输入特殊字符') },
        {
          validator(_rule, value) {
            return new Promise((resolve, reject) => {
              if (!value || value === '') return resolve();
              checkLoginCode(record.value.loginCode || '', value)
                .then((res) => (res ? resolve() : reject(t('登录账号已存在'))))
                .catch((err) => reject(err.message || t('验证失败')));
            });
          },
        },
      ],
    },
    {
      label: t('用户昵称'),
      field: 'userName',
      component: 'Input',
      componentProps: {
        maxlength: 32,
      },
      required: true,
    },
    // {
    //   label: t('电子邮箱'),
    //   field: 'email',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 300,
    //   },
    //   ifShow: () => op.value === 'add' || op.value === 'edit',
    //   rules: [{ type: 'email', message: t('请填写正确的邮箱地址') }],
    // },
    {
      label: t('手机号码'),
      field: 'mobile',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      ifShow: op.value === 'add' || op.value === 'edit' || op.value === 'auth',
    },
    // {
    //   label: t('办公电话'),
    //   field: 'phone',
    //   component: 'Input',
    //   componentProps: {
    //     maxlength: 100,
    //   },
    //   ifShow: () => op.value === 'add' || op.value === 'edit',
    // },
    {
      label: t('用户类型'),
      field: 'customerType',
      component: 'RadioGroup',
      defaultValue: 'teacher',
      componentProps: {
        dictType: 'sys_customer_type',
      },
      ifShow: () => {
        return op.value === 'add' || op.value === 'edit' || op.value === 'auth';
      },
      dynamicDisabled: () => {
        return op.value != 'add';
      },
    },
    // {
    //   label: t('权重'),
    //   field: 'userWeight',
    //   helpMessage: '排序，权重越大排名越靠前，请填写数字。',
    //   component: 'InputNumber',
    //   defaultValue: '',
    //   componentProps: {
    //     maxlength: 8,
    //   },
    //   ifShow: () => op.value === 'add' || op.value === 'edit',
    // },
    {
      label: t('详情信息(老师)'),
      field: 'vteacher',
      component: 'FormGroup',
      colProps: { lg: 24, md: 24 },
      ifShow: ({ values }) => {
        return values.customerType == 'teacher' && (op.value === 'add' || op.value === 'edit');
      },
    },
    {
      label: t('姓名'),
      field: 'vteacher.teacherName',
      component: 'Input',
      componentProps: {
        maxlength: 32,
      },
      required: true,
      ifShow: ({ values }) => {
        return values.customerType == 'teacher' && (op.value === 'add' || op.value === 'edit');
      },
    },
    {
      label: t('工号'),
      field: 'vteacher.jobNumber',
      component: 'Input',
      componentProps: {
        maxlength: 32,
      },
      rules: [
        {
          validator(_rule, value) {
            return new Promise((resolve, reject) => {
              if (!value || value === '') return resolve();
              checkEmpNo(record.value.employee?.empNo || '', value)
                .then((res) => (res ? resolve() : reject(t('员工工号已存在'))))
                .catch((err) => reject(err.message || t('验证失败')));
            });
          },
        },
      ],
      ifShow: ({ values }) => {
        return values.customerType == 'teacher' && (op.value === 'add' || op.value === 'edit');
      },
    },
    {
      label: t('教师类型'),
      field: 'vteacher.teacherType',
      component: 'RadioGroup',
      componentProps: {
        dictType: '	vke_teacher_type',
        allowClear: true,
      },
      ifShow: ({ values }) => {
        return values.customerType == 'teacher' && (op.value === 'add' || op.value === 'edit');
      },
    },
    {
      label: t('出生年月'),
      field: 'vteacher.dateBirth',
      component: 'DatePicker',
      componentProps: {
        dictType: 'sys_user_sex',
        allowClear: true,
      },
      ifShow: ({ values }) => {
        return values.customerType == 'teacher' && (op.value === 'add' || op.value === 'edit');
      },
    },
    {
      label: t('性别'),
      field: 'vteacher.sex',
      component: 'RadioGroup',
      // defaultValue: '1',
      componentProps: {
        dictType: 'sys_user_sex',
        allowClear: true,
      },
      ifShow: ({ values }) => {
        return values.customerType == 'teacher' && (op.value === 'add' || op.value === 'edit');
      },
    },
    {
      label: t('职称'),
      field: 'userType',
      component: 'Select',
      componentProps: {
        dictType: 'sys_user_type',
        allowClear: true,
      },
      ifShow: ({ values }) => {
        return values.customerType == 'teacher' && (op.value === 'add' || op.value === 'edit');
      },
    },
    {
      label: t('代课科目'),
      field: 'vteacher.substituteSubjects',
      component: 'Input',
      componentProps: {
        maxlength: 32,
      },
      ifShow: ({ values }) => {
        return values.customerType == 'teacher' && (op.value === 'add' || op.value === 'edit');
      },
    },
    {
      label: t('是否班主任'),
      field: 'vteacher.isHeadmaster',
      component: 'RadioGroup',
      componentProps: {
        dictType: '	sys_yes_no',
        allowClear: true,
      },
      ifShow: ({ values }) => {
        return values.customerType == 'teacher' && (op.value === 'add' || op.value === 'edit');
      },
    },
    {
      label: t('代课年级'),
      field: 'vteacher.substituteGrade',
      component: 'Select',
      componentProps: {
        dictType: 'vke_grade',
        allowClear: true,
      },
      ifShow: ({ values }) => {
        return values.customerType == 'teacher' && (op.value === 'add' || op.value === 'edit');
      },
    },
    {
      label: t('代课班级'),
      field: 'vteacher.substituteClass',
      component: 'Select',
      componentProps: {
        dictType: 'vke_class',
        allowClear: true,
      },
      ifShow: ({ values }) => {
        return values.customerType == 'teacher' && (op.value === 'add' || op.value === 'edit');
      },
    },
    {
      label: t('头像'),
      field: 'vteacher.headPortrait',
      component: 'Uploads',
      componentProps: {
        multiple: false,
        maxNumber: 1,
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'teacherUser_avatar',
        uploadType: 'image',
        onChange: (e: ChangeEvent) => {
          console.log(e.fileUrl);
          avatar.value = e.fileUrl;
          return avatar.value;
        },
      },
      ifShow: ({ values }) => {
        return values.customerType == 'teacher' && (op.value === 'add' || op.value === 'edit');
      },
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('履历'),
      field: 'vteacher.resume',
      component: 'Input',
      colProps: { lg: 24, md: 24 },
      slot: 'teacherResume',
      show: ({ values }) => {
        return values.customerType == 'teacher' && (op.value === 'add' || op.value === 'edit');
      },
    },
    {
      label: t('详情信息(学生)'),
      field: 'vstudent',
      component: 'FormGroup',
      colProps: { lg: 24, md: 24 },
      ifShow: ({ values }) => {
        return values.customerType == 'student' && (op.value === 'add' || op.value === 'edit');
      },
    },
    {
      label: t('姓名'),
      field: 'vstudent.studentName',
      component: 'Input',
      componentProps: {
        maxlength: 300,
      },
      required: true,
      ifShow: ({ values }) => {
        return (
          values.customerType == 'student' &&
          (op.value === 'add' || op.value === 'edit' || op.value === 'auth')
        );
      },
    },
    {
      label: t('学号'),
      field: 'vstudent.studentNumber',
      component: 'Input',
      componentProps: {
        maxlength: 32,
      },
      required: true,
      ifShow: ({ values }) => {
        return (
          values.customerType == 'student' &&
          (op.value === 'add' || op.value === 'edit' || op.value === 'auth')
        );
      },
    },
    {
      label: t('性别'),
      field: 'vstudent.sex',
      component: 'RadioGroup',
      // defaultValue: '1',
      componentProps: {
        dictType: 'sys_user_sex',
        allowClear: true,
      },
      required: true,
      ifShow: ({ values }) => {
        return (
          values.customerType == 'student' &&
          (op.value === 'add' || op.value === 'edit' || op.value === 'auth')
        );
      },
    },
    {
      label: t('出生年月'),
      field: 'vstudent.dateBirth',
      component: 'DatePicker',
      componentProps: {
        dictType: 'sys_user_sex',
        allowClear: true,
      },
      ifShow: ({ values }) => {
        return values.customerType == 'student' && (op.value === 'add' || op.value === 'edit');
      },
    },
    {
      label: t('年级'),
      field: 'vstudent.grade',
      component: 'Select',
      componentProps: {
        dictType: '	vke_grade',
        allowClear: true,
      },
      ifShow: ({ values }) => {
        return values.customerType == 'student' && (op.value === 'add' || op.value === 'edit');
      },
    },
    {
      label: t('班级'),
      field: 'vstudent.classCode',
      component: 'Select',
      componentProps: {
        dictType: '	vke_class',
        allowClear: true,
      },
      ifShow: ({ values }) => {
        return values.customerType == 'student' && (op.value === 'add' || op.value === 'edit');
      },
    },
    {
      label: t('入学时间'),
      field: 'vstudent.entranceTime',
      component: 'DatePicker',
      componentProps: {
        maxlength: 32,
      },
      ifShow: ({ values }) => {
        return values.customerType == 'student' && (op.value === 'add' || op.value === 'edit');
      },
    },
    {
      label: t('学籍状态'),
      field: 'vstudent.status',
      component: 'Select',
      componentProps: {
        dictType: 'vke_student_status',
        allowClear: true,
      },
      ifShow: ({ values }) => {
        return values.customerType == 'student' && (op.value === 'add' || op.value === 'edit');
      },
    },
    {
      label: t('头像'),
      field: 'vstudent.headPortrait',
      component: 'Uploads',
      componentProps: {
        multiple: false,
        maxNumber: 1,
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'studentUser_avatar',
        uploadType: 'image',
        onChange: (e: ChangeEvent) => {
          console.log(avatar.value);
          avatar.value = e.fileUrl;
          return avatar.value;
        },
      },
      ifShow: ({ values }) => {
        return values.customerType == 'student' && (op.value === 'add' || op.value === 'edit');
      },
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('家长'),
      field: 'vstudent.employeeOfficeList',
      component: 'Input',
      colProps: { lg: 24, md: 24 },
      slot: 'userTeacher',
      show: ({ values }) => {
        return (
          values.customerType == 'student' &&
          (op.value === 'add' || op.value === 'edit' || op.value === 'auth')
        );
      },
    },
    {
      label: t('角色信息'),
      field: 'roleInfo',
      component: 'FormGroup',
      colProps: { lg: 24, md: 24 },
      ifShow: () => op.value === 'auth',
    },
    {
      label: t('分配角色'),
      field: 'userRoleString',
      component: 'Input',
      colProps: { lg: 24, md: 24 },
      slot: 'userRoleString',
      show: () => op.value === 'auth',
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, updateSchema, validate }] = useForm({
    schemas: inputFormSchemas,
    baseColProps: { lg: 12, md: 24 },
    labelWidth: 120,
  });

  const [registerEmployeeOfficeTable, employeeOfficeTable] = useTable({
    actionColumn: {
      width: 60,
      actions: (record: Recordable) => [
        {
          icon: 'ant-design:delete-outlined',
          color: 'error',
          popConfirm: {
            title: '是否确认删除',
            confirm: handleEmployeeOfficeDelete.bind(this, record),
          },
          auth: 'sys:empUser:edit',
        },
      ],
    },
    rowKey: 'id',
    pagination: false,
    bordered: true,
    size: 'small',
    inset: true,
  });

  async function setEmployeeOfficeTableData(res: Recordable) {
    employeeOfficeTable.setColumns([
      {
        title: t('时间'),
        dataIndex: 'startTime',
        width: 100,
        align: 'left',
        editRow: true,
        editComponent: 'DatePicker',
        editRule: true,
      },
      {
        title: t('履历描述'),
        dataIndex: 'teacherDescribe',
        width: 100,
        align: 'left',
        editRow: true,
        editComponent: 'Input',
        editRule: true,
      },
    ]);
    employeeOfficeTable.setTableData(record.value.vteacherResumes || []);
  }
  async function setuserTeacherTable(res: Recordable) {
    userTeacherTable.setColumns([
      {
        title: t('家长类型'),
        dataIndex: 'relationName',
        width: 260,
        align: 'center',
        editRow: true,
        editComponent: 'Select',
        editComponentProps: {
          dictType: 's_index_parents',
        },
        editRule: true,
        slots: { customRender: 'userRole' },
      },
      {
        title: t('电话'),
        dataIndex: 'relationPhone',
        width: 260,
        align: 'center',
        editRow: true,
        editComponent: 'Input',
        editRule: true,
      },
    ]);
    if (record.value.vstudent) {
      userTeacherTable.setTableData(record.value.vstudent.parentList);
    } else {
      userTeacherTable.setTableData([]);
    }
  }
  function handleEmployeeOfficeRowClick(record: Recordable) {
    record.onEdit?.(true, false);
  }

  function handleEmployeeOfficeAdd() {
    employeeOfficeTable.insertTableDataRecord({
      id: 'rid_' + new Date().getTime(),
      editable: true,
    });
  }
  function handleParentsOfficeAdd(){
    userTeacherTable.insertTableDataRecord({
      id: 'rid_' + new Date().getTime(),
      editable: true,
    });
  }

  function handleEmployeeOfficeDelete(record: Recordable) {
    employeeOfficeTable.deleteTableDataRecord(record);
  }
  function handleDelete(record: Recordable) {
    userTeacherTable.deleteTableDataRecord(record);
  }

  async function getEmployeeOfficeList() {
    let employeeOfficeListValid = true;
    let employeeOfficeList = employeeOfficeTable.getDataSource();
    for (const record of employeeOfficeList) {
      if (!(await record.onEdit?.(false, true))) {
        employeeOfficeListValid = false;
      }
    }
    if (!employeeOfficeListValid) {
      throw new Error('employeeOfficeList valid.');
    }
    return employeeOfficeList;
  }

  async function getuserTeacherTableList() {
    let employeeOfficeListValid = true;
    let userTeacherTableList = userTeacherTable.getDataSource();
    for (const record of userTeacherTableList) {
      if (!(await record.onEdit?.(false, true))) {
        employeeOfficeListValid = false;
      }
    }
    if (!employeeOfficeListValid) {
      throw new Error('userTeacherTableList valid.');
    }
    return userTeacherTableList;
  }
  const [registerUserTeacherTable, userTeacherTable] = useTable({
    actionColumn: {
      width: 60,
      actions: (record: Recordable) => [
        {
          icon: 'ant-design:delete-outlined',
          color: 'error',
          popConfirm: {
            title: '是否确认删除',
            confirm: handleDelete.bind(this, record),
          },
          auth: 'sys:empUser:edit',
        },
      ],
    },
    rowSelection: { type: 'checkbox' },
    pagination: false,
    bordered: true,
    size: 'small',
    inset: true,
  });
  // async function getuserRoleTableList() {
  //   let employeeOfficeListValid = true;
  //   let userRoleTableList = UserRoleTable.getDataSource();
  //   for (const record of userRoleTableList) {
  //     if (!(await record.onEdit?.(false, true))) {
  //       employeeOfficeListValid = false;
  //     }
  //   }
  //   if (!employeeOfficeListValid) {
  //     throw new Error('userTeacherTableList valid.');
  //   }
  //   return userRoleTableList;
  // }

  const [registerUserRoleTable, userRoleTable] = useTable({
    columns: [
      {
        title: t('角色名称'),
        dataIndex: 'name',
        width: 260,
        align: 'center',
      },
      {
        title: t('角色编码'),
        dataIndex: 'id',
        width: 260,
        align: 'center',
      },
    ],
    rowSelection: { type: 'checkbox' },
    pagination: false,
    bordered: true,
    size: 'small',
    inset: true,
  });
  async function setUserRoleTableData(res: Recordable) {
    const dataSource = await roleTreeData({
      userType: record.value.userType,
      ctrlPermi: ctrlPermi.value,
    });
    userRoleTable.setTableData(dataSource || []);
    userRoleTable.setSelectedRowKeys(res.roleList?.map((item) => item.id) || []);
  }

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    resetFields();
    employeeOfficeTable.setTableData([]);
    userTeacherTable.setTableData([]);
    userTeacherTable.setSelectedRowKeys([]);
    setDrawerProps({ loading: true });
    op.value = data.op || 'add';
    let res;
    if (data.userType == 'teacher') {
      res = await teacherUserForm(data);
      res.teacherUser.customerType = data.userType;
      record.value = (res.teacherUser || {}) as TeacherUser;
    } else if (data.userType == 'student') {
      res = await studentUserForm(data);
      res.studentUser.customerType = data.userType;
      record.value = (res.studentUser || {}) as StudentUser;
    }
    if (data.op == 'add') {
      record.value = [];
      record.value.isNewRecord = true;
    } else {
      record.value.isNewRecord = false;
    }
    ctrlPermi.value = '2';
    setFieldsValue(record.value);
    updateSchema([
      {
        field: 'loginCode',
        componentProps: {
          disabled: op.value === 'auth',
        },
      },
      {
        field: 'userName',
        componentProps: {
          disabled: op.value === 'auth',
        },
      },
    ]);
    if (op.value === 'add' || op.value === 'edit') {
      setEmployeeOfficeTableData(res);
      updateSchema([
        {
          field: 'employee.office.officeCode',
          componentProps: {
            api: officeTreeData,
            params: { ctrlPermi },
            canSelectParent: false,
          },
        },
        {
          field: 'resume.company.companyCode',
          componentProps: {
            api: companyTreeData,
            params: { ctrlPermi },
            canSelectParent: false,
          },
        },
        // {
        //   field: 'resume.teacherDescribe',
        //   componentProps: {
        //     options: res.teacherResume.map((item) => ({
        //       value: item.teacherDescribe,
        //     })),
        //     mode: 'multiple',
        //   },
        // },
      ]);
    }
    if (op.value === 'add' || op.value === 'edit') {
      setuserTeacherTable(res);
    }
    if (op.value === 'add' || op.value === 'auth') {
      setUserRoleTableData(res);
    }
    setDrawerProps({ loading: false });
  });
  function updateAvatar(option){
    console.log(option);
  }

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        userCode: record.value.userCode,
        op: op.value,
      };
      data.userType = 'employee';
      if (op.value === 'add' || op.value === 'edit') {
        // if (!data.employee.empNo) {
        //   data.employee.empNo = record.value.loginCode;
        // }
        let resume = await getEmployeeOfficeList();
        // resume.forEach((item,index)=>{
        //   // item.teacherId = record.value.vteacher.teacherId;
        // });
        resume = JSON.stringify(resume);
        if (JSON.parse(resume).length > 0) {
          data.vteacher.resume = resume;
        }
      }
      if (op.value === 'add' || op.value === 'auth') {
        data.userRoleString = userRoleTable.getSelectRowKeys().join(',');
      }
      let res;
      if (data.customerType == 'teacher') {
        data.userType = 'teacher';
        data.teacherUser_avatar = avatar.value;
        data.vteacher.headPortrait = avatar.value;

        // data.teacherId
        delete data.vstudent;
        if (op.value === 'edit') {
          data.vteacher.teacherId = record.value.vteacher.teacherId;
        }
        res = await teacherUserSave(params, data);
      } else if (data.customerType == 'student') {
        data.vstudent.headPortrait = avatar.value;
        let parent = await getuserTeacherTableList();
        let parentInfo = [];
        parent.forEach((item,index) => {
          let info;
          if (item.relationCode) {
            info = {
              relationName: item.relationName,
              // relationId: item.relationId,
              relationPhone: item.relationPhone,
              relationCode: item.relationCode,
              studentId: record.value.userCode,
            };
          } else {
            info = {
              // relationId: item.relationId,
              relationPhone: item.relationPhone,
              relationCode: item.relationName,
              studentId: record.value.userCode,
            };
          }
          parentInfo.push(info);
        });
        if (parentInfo) {
          data.vstudent.parentInfo = JSON.stringify(parentInfo);
        }
        delete data.vteacher;
        data.userType = 'student';
        if (op.value === 'edit') {
          data.vstudent.studentId = record.value.vstudent.studentId;
        }
        data.userName = data.vstudent.studentName;
        res = await studentUserSave(params, data);
      }
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
